import ScreenPositionUtil from "./ScreenPositionUtil";
import GameConfig from "./GameConfig";
import ContentArea from "./ContentArea";
import testJson from "./testJson";


/**
 * 网格区域
 */
export default class ContentAreaRoot extends Laya.Sprite {
    constructor(inParent) {
        super();
        this.parentMain = inParent;
        this.contentAreaDatas = [];
        this.contentAreaViews = [];

        this.pos(GameConfig.RootStartX, GameConfig.RootStartY);
    }

    initByContentAreaDatas(inData) {
        this.contentAreaDatas = inData;
        for (let i = 0; i < this.contentAreaDatas.length; i++) {
            let areaData = this.contentAreaDatas[i];
            let tempArea = new ContentArea;
            tempArea.initByContentAreaData(areaData);

            this.addChild(tempArea);
            this.contentAreaViews.push(tempArea);
        }
    }

    tryAddPicInArea(inData) {
        let re = false;

        if(inData.imageGridVSum == inData.imageGridHSum == 1) {
            for (let i = 0; i < this.contentAreaViews.length; i++) {
                let tempArea = this.contentAreaViews[i];
                if(tempArea.getAreaData().areaWidth == tempArea.getAreaData().areaHeight == 1 && tempArea.getAreaData().type == "imge") {
                    let addAreaIndex = -1;
                    addAreaIndex = tempArea.checkIsHaveEnoughGapInAreaBeforeCreate(inData.imageGridList);
                    if (addAreaIndex >= 0) {
                        tempArea.addPicByData(inData, addAreaIndex);
                        return true;
                    }
                }
            }
        }


        for (let i = 0; i < this.contentAreaViews.length; i++) {
            let tempArea = this.contentAreaViews[i];

            if (tempArea.getAreaData().type == "imge") {
                let addAreaIndex = -1;
                addAreaIndex = tempArea.checkIsHaveEnoughGapInAreaBeforeCreate(inData.imageGridList);
                console.log("addAreaIndex", addAreaIndex)
                if (addAreaIndex >= 0) {
                    tempArea.addPicByData(inData, addAreaIndex);
                    re = true;
                    break;
                }
            }
        }
        return re;
    }

    tryAddTextInArea(inData) {
        let re = false;
        let tempTextAreaSum = 0;
        let tempAreaIndexs = [];
        for (let i = 0; i < this.contentAreaViews.length; i++) {
            let tempArea = this.contentAreaViews[i];
            if (tempArea.getAreaData().type == "text") {
                tempTextAreaSum++;
                tempAreaIndexs.push(i);
            }
        }

        if (tempTextAreaSum == 0)
            return re;

        if (tempTextAreaSum == 1) {
            this.contentAreaViews[tempAreaIndexs[0]].addTextByData(inData);
        } else {
            for (let i = 0; i < inData.length; i++) {
                this.contentAreaViews[tempAreaIndexs[i]].addTextByData([inData[i]]);
            }
        }
        return true;
    }


    updateSelectState(exceptView) {
        for (let i = 0; i < this.numChildren; i++) {
            let temp = this.getChildAt(i);
            if (temp == exceptView || temp.getAreaData().type == "text")
                continue;

            temp.updateSelectState(null, false);
        }
    }

    setBorderAndTipVisible(e) {
        for (let i = 0; i < this.numChildren; i++) {
            let temp = this.getChildAt(i);
            temp.setBorderVisible(e);
            temp.updateSelectState(null,false);
            if(!e)
                temp.setAreaTipVisible(e);
        }

        if(e)
            this.updateAreaTips();
    }


    updateAreaTips() {
        let needTips = true;
        for (let i = 0; i < this.numChildren; i++) {
            let tempArea = this.getChildAt(i);
            if (tempArea.picRootView.numChildren > 0) {
                needTips = false;
                break;
            }
        }

        for (let i = 0; i < this.numChildren; i++) {
            let tempArea = this.getChildAt(i);
            tempArea.setAreaTipVisible(needTips)
        }
    }

    clearContents() {
        for (let i = 0; i < this.numChildren; i++) {
            let tempArea = this.getChildAt(i);
            tempArea.picRootView.removeChildren(0,1000);
        }

        this.updateAreaTips();
    }
}

